package com.wy2.eclipse.tool.lib;

import java.io.File;
import java.io.Serializable;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wy2.eclipse.tool.lib.common.IDEUtils;


/**
 * 项目导入
 * @author wuyy
 * @date 2018年1月4日上午11:54:47
 *
 */
public class ImportProject {
	
	private static final Logger logger = LoggerFactory.getLogger(ImportProject.class);
	
	/**
	 * 导入项目
	 * @param projectFileName  eclipse项目文件名称：全路径
	 * @return
	 */
	public  ImportProjectResult importProject(File projectFile){
	    ImportProjectResult result=new ImportProjectResult();
	    if(!projectFile.exists()) {
	        result.setSuccess(false);
            result.setMessage("项目文件不存在");
            return result;
	    }
		ProjectRecord record = new ProjectRecord(projectFile);
		IProject project =IDEUtils.getWorkspaceProject(record.projectName);
		result.setProjectName(record.getProjectName());
		
		if(project.exists()){
		    
		    result.setSuccess(false);
		    result.setMessage("项目已经存在");
		    return result;
		}
		
		
		try {
			project.create(record.description, null);
			project.open(null);
			
			result.setSuccess(true);
            result.setMessage("导入项目成功");
		} catch (CoreException e) {
            logger.error("导入项目失败", e);
            result.setSuccess(false);
            result.setMessage("导入项目失败");
		}
		return result;
	}
	
	public class ProjectRecord {
		File projectSystemFile;

		Object projectArchiveFile;

		String projectName;

		Object parent;

		int level;
		
		boolean hasConflicts;

		IProjectDescription description;

		/**
		 * Create a record for a project based on the info in the file.
		 * 
		 * @param file
		 */
		ProjectRecord(File file) {
			projectSystemFile = file;
			setProjectName();
		}

		/**
		 * @param file
		 * 		The Object representing the .project file
		 * @param parent
		 * 		The parent folder of the .project file
		 * @param level
		 * 		The number of levels deep in the provider the file is
		 */
		ProjectRecord(Object file, Object parent, int level) {
			this.projectArchiveFile = file;
			this.parent = parent;
			this.level = level;
			setProjectName();
		}

		/**
		 * Set the name of the project based on the projectFile.
		 */
		private void setProjectName() {
			try {
				// If we don't have the project name try again
				if (projectName == null) {
					IPath path = new Path(projectSystemFile.getPath());
					// if the file is in the default location, use the directory
					// name as the project name
					if (isDefaultLocation(path)) {
						projectName = path.segment(path.segmentCount() - 2);
						description = ResourcesPlugin.getWorkspace().newProjectDescription(projectName);
					} else {
						description = ResourcesPlugin.getWorkspace().loadProjectDescription(path);
						projectName = description.getName();
					}

				}
			} catch (CoreException e) {
				// no good couldn't get the name
			}
		}

		/**
		 * Returns whether the given project description file path is in the
		 * default location for a project
		 * 
		 * @param path
		 * 		The path to examine
		 * @return Whether the given path is the default location for a project
		 */
		private boolean isDefaultLocation(IPath path) {
			// The project description file must at least be within the project,
			// which is within the workspace location
			if (path.segmentCount() < 2)
				return false;
			return path.removeLastSegments(2).toFile().equals(Platform.getLocation().toFile());
		}

		/**
		 * Get the name of the project
		 * 
		 * @return String
		 */
		public String getProjectName() {
			return projectName;
		}

		/**
		 * @return Returns the hasConflicts.
		 */
		public boolean hasConflicts() {
			return hasConflicts;
		}
	}
	
	/**
	 * 项目导入结果
	 * @author wuyy
	 * @date 2018年1月4日下午12:00:16
	 *
	 */
	public class ImportProjectResult implements Serializable{
	    /**
         * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
         */
        private static final long serialVersionUID = 1L;
        
        private String projectName;
        private Boolean success;
	    private String message;
	    
	    
	    
	    
        public String getProjectName() {
            return projectName;
        }
        public void setProjectName(String projectName) {
            this.projectName = projectName;
        }
        public Boolean getSuccess() {
            return success;
        }
        public void setSuccess(Boolean success) {
            this.success = success;
        }
        public String getMessage() {
            return message;
        }
        public void setMessage(String message) {
            this.message = message;
        }
	    
	}
}
